<?php


class System_InfoController extends WSB_Controller_Action
{

	public function init()
	{
		parent::init(); // immer zuerst!
		$this->_helper->viewRenderer->setNoRender(true);

		$navigation = $this->_helper->getHelper('Navigation');
		$navigation->add(WSB_Controller_Url::getUrl('info', 'index', null, 'system'), 'PHP Info');
		$navigation->add(WSB_Controller_Url::getUrl('info', 'errorlog', null, 'system'), 'Errorlog');

	}

	public function indexAction()
	{
		ob_start();
		phpinfo();
		preg_match ('%<style type="text/css">(.*?)</style>.*?(<body>.*</body>)%s', ob_get_clean(), $matches);
		$content = str_replace('<table ', '<table border="1" ', $matches[2]);
		$this->_response->appendBody($content);
	}

	public function errorlogAction()
	{
		$this->_response->appendBody('<h1>Latest log entries</h1>');
		$errorlog = ini_get('error_log');
		if(is_readable($errorlog)) {
			$filesize = filesize ($errorlog);
			if($filesize > 0) {
				$lines = 0;
				$handle = fopen ($errorlog, "r");
				$position = $filesize;
				$i = 0;
				$str = '';
				while($position >= 0) {
					fseek($handle, $position);
					$char = fread($handle, 1);
					if($char == "\n") {
						$char = "<br /><br />";
						$lines++;
					}
					$str =  $char . $str;
					$position--;
					$i++;
					if($lines > 500) break;
				}
				fclose ($handle);

				if($lines > 0) {
					$this->_response->appendBody($str);
				} else {
					$this->_response->appendBody(sprintf("The logfile '%s' is empty.", $errorlog));
				}
			} else {
				$this->_response->appendBody(sprintf("The logfile '%s' is empty.", $errorlog));
			}
		} else {
			$this->_response->appendBody(sprintf("Error: The logfile '%s' is not readable.", $errorlog));
		}
	}

}
